<?php
namespace backend\actions;
use backend\component\BaseAction;
use backend\component\ResultFormat;

class ModifyAction extends BaseAction
{

    public function run() {
        $id = \Yii::$app->request->get("id",0);
        $params = \Yii::$app->request->post("form");
        $model = $this->findModel($id);
        $model->setAttributes($params);
        if (!$model->save()) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("操作成功");
    }

    private function findModel($id) {
        if (!$id) {
            $model = $this->modelObj;
            $model->setIsNewRecord(true);
            return $model;
        } else {
            $model =  $this->modelObj->findOne($id);
            $model->setIsNewRecord(false);
            return $model;
        }

    }

}